package com.lky.mall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.lky.mall.product.service.AttrAttrgroupRelationService;
import com.lky.mall.product.service.CategoryService;
import com.lky.mall.product.vo.AttrGroupRelationVo;
import com.lky.mall.product.vo.AttrRespVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.lky.mall.product.entity.AttrGroupEntity;
import com.lky.mall.product.service.AttrGroupService;
import com.lky.common.utils.PageUtils;
import com.lky.common.utils.R;



/**
 * 属性分组
 *
 * @author laikeyi
 * @email laikeyi@gmail.com
 * @date 2025-07-21 01:39:51
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;
    /**
     * 列表
     */
    /*
    * page: 1,//当前页码
    * limit: 10,//每页记录数
    * sidx: 'id',//排序字段
    * order: 'asc/desc'.//排序方式
    * key: '华为'//检索关键字
    * */
    /*
    * catelogId是菜单的id,点击三级菜单就返回该菜单所有数据
    * */
    @RequestMapping("/list/{catelogId}")
    @RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable("catelogId") Long catelogId){
        PageUtils page = attrGroupService.queryPage(params, catelogId);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    @RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

		Long catelogId = attrGroup.getCatelogId();
        Long[] path = categoryService.findCatelogPath(catelogId);

        attrGroup.setCatelogPath(path);

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 传入属性id（attrId）和组id（attrGroupId）以新增关联关系
     * 对AttrgroupRelation关联表进行保存
     */
    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRelationVo> vos){
        attrAttrgroupRelationService.saveBatch(vos);
        return R.ok();
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
